﻿using System;
using System.Collections.Generic;
using System.Text;
using Savage.DotNetLibrary.DatabaseLibrary;
using NUnit.Framework;

namespace Savage.DotNetLibrary.TestDotNetLibrary.DatabaseLibraryTests
{
    [TestFixture]
    public class TestLogins
    {
        private string connectionString = @"Server=.\SQLEXPRESS;Trusted_Connection=true";

        [Test]
        public void TestLoginsMethod()
        {            
            Logins login = new Logins(connectionString);
            string loginName = Guid.NewGuid().ToString().Replace("-", "");
            string password = Guid.NewGuid().ToString();

            login.CreateLogin(loginName, password);

            string newConnectionString = @"Server=.\SQLEXPRESS;Uid=" + loginName + ";Pwd=" + password;
            Console.Out.WriteLine(newConnectionString);
            //Try and connect using the new login
            System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(newConnectionString);
            try
            {
                connection.Open();
                Assert.AreEqual(System.Data.ConnectionState.Open, connection.State);
            }
            finally
            {
                connection.Close();
                //Clear the pool for this connection to avoid the error where you can't drop the login because it is logged in
                System.Data.SqlClient.SqlConnection.ClearPool(connection);
            }

            login.DropLogin(loginName);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestCreateLoginArgumentNull()
        {
            Logins logins = new Logins(connectionString);
            logins.CreateLogin(null, null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestDropLoginArgumentNull()
        {
            Logins logins = new Logins(connectionString);
            logins.DropLogin(null);
        }

        [Test]
        public void TestCreateLoginMustChange()
        {
            string loginName = Guid.NewGuid().ToString();
            string password = Guid.NewGuid().ToString();

            Logins logins = new Logins(connectionString);
            logins.CreateLogin(loginName, password, true);

            string newConnectionString = @"Server=.\SQLEXPRESS;Uid=" + loginName + ";Pwd=" + password;
            Console.Out.WriteLine(newConnectionString);
            //Try and connect using the new login
            System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(newConnectionString);
            int errorId = 0;
            try
            {
                connection.Open();
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                errorId = ex.ErrorCode;
            }
            finally
            {
                connection.Close();
                //Clear the pool for this connection to avoid the error where you can't drop the login because it is logged in
                System.Data.SqlClient.SqlConnection.ClearPool(connection);
            }
            
            logins.DropLogin(loginName);

            //Checkt hat the error message thrown was expected
            Assert.AreEqual(-2146232060, errorId);
        }

        [Test]
        public void TestCreateLoginDefaultDatabase()
        {
            string loginName = Guid.NewGuid().ToString();
            string password = Guid.NewGuid().ToString();

            Logins logins = new Logins(connectionString);
            logins.CreateLogin(loginName, password, false, "master");

            string newConnectionString = @"Server=.\SQLEXPRESS;Uid=" + loginName + ";Pwd=" + password;
            Console.Out.WriteLine(newConnectionString);
            //Try and connect using the new login
            System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(newConnectionString);
            try
            {
                connection.Open();
                Assert.AreEqual(System.Data.ConnectionState.Open, connection.State);
            }
            finally
            {
                connection.Close();
                //Clear the pool for this connection to avoid the error where you can't drop the login because it is logged in
                System.Data.SqlClient.SqlConnection.ClearPool(connection);
            }
            logins.DropLogin(loginName);
        }
    }
}
